#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;

struct edge{
	int  f, t, w;
}ed[1002000];
int  n, m, et, ma, c, u[100020];

bool cmp(edge a,edge b){
	return a.w < b.w;
}

int getf(int x){
	if(u[x] == x) return x;
	else u[x]=getf(u[x]); 
	return u[x];
}

void mg(int x,int y){
	int xf = getf(x),yf = getf(y);
	if(xf != yf) {
		u[xf] = u[yf];
	}
	return;
}

int get_num() {
	int ans = 0;
	char tmp = getchar();
	while (tmp < '0' || tmp > '9') tmp = getchar();
	while (tmp <= '9' && tmp >= '0') {
		ans = ans * 10 + tmp - 48;
		tmp = getchar();
	}
	return ans;
}

int main() {
	cin >> n >> m;
	for(int i = 1; i <= n; i++) u[i] = i;
	for(int i = 1; i <= m; i++) {
		//int x, y, z;
		//cin >> x >> y >> z;
		ed[et].f = get_num();
		ed[et].t = get_num();
		ed[et].w = get_num();
		et++;
	}
	sort(ed, ed + et, cmp);
	/*printf("\n\n\n"); 
	for(int i = 0; i < et; i++) {
		printf("%d %d %d \n", ed[i].f, ed[i].t, ed[i].w);
	}*/
	for(int i = 0; i < et; i++) {
		int x = ed[i].f;
		int y = ed[i].t;
		if(getf(x) == getf(y)) continue;
		int z = ed[i].w;
		mg(x, y);
		ma += z;
		c++;
		if(c == n) break;
	}
	printf("%d\n", ma);
	return 0;
}

